//
//  NSString+Converter.m
//  WeiZi
//
//  Created by Meniny on 16/3/16.
//  Copyright © 2016年 Wiiith. All rights reserved.
//
//  Powerd by Meniny.
//  See http://www.meniny.cn/ for more informations.
//

#import "NSString+Converter.h"

@implementation NSString (Converter)

- (NSString *)UTF8StringFromISO8591 {
    NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(0x0201);//kCFStringEncodingISOLatin1
    const char *cString = [self cStringUsingEncoding:encoding];
    NSLog(@"%s", cString);
    if (cString != NULL) {
        return [NSString stringWithUTF8String:cString];
    }
    return self;
}

#pragma mark - 汉字与utf8相互转化
- (NSString *)chineseToUTF8String {
    return [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

- (NSString *)UTF8ToChineseString {
    return [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
-(NSString *)getConfigerTagsStringWithMaxWidth:(CGFloat)maxWidth font:(UIFont *)font tagCount:(NSInteger)count{
    
    NSMutableString *tempStr = [NSMutableString stringWithFormat:@"%@",self];
    
    
    for (int i = 0; i<count; i++) {
        CGSize size = CGSizeMake(10,30);
        if (size.width > maxWidth) {
            
            NSRange range = [tempStr rangeOfString:@" | "];
            if (range.length == 0) {
                break;
            }
            [tempStr deleteCharactersInRange:NSMakeRange(range.location, tempStr.length-range.location)];
            
        }else{
            break;
        }
        
    }
    
    return tempStr;
}

//计算评论框文字的高度
+(CGFloat) heightForTextView: (UITextView *)textView font:(UIFont *)font WithText: (NSString *) strText{
    //    float padding = 10.0;
    CGSize constraint = CGSizeMake(textView.contentSize.width, CGFLOAT_MAX);
    CGRect size = [strText boundingRectWithSize:constraint
                                        options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                     attributes:@{NSFontAttributeName: font}
                                        context:nil];
    float textHeight = size.size.height;
    return textHeight+20;
}


@end
